import 'package:flutter/material.dart';
import 'package:top_foundation/top_foundation.dart';

class CalendarViewState {
  var bgColor = Colors.transparent;
  var isRange = false;

  var start = DateModel.zero().obs;
  var end = DateModel.zero().obs;
  var now = DateModel.zero();
  var curr = DateModel.zero().obs;

  var isShowYearView = false;

  late CalendarResult result;
}

class DateModel {
  int year;
  List<MonthModel> months;
  MonthModel month;
  int get lastYear {
    if (month.month == 1) {
      return year - 1;
    }
    return year;
  }

  int get nextYear {
    if (month.month == 12) {
      return year + 1;
    }
    return year;
  }

  DateModel({
    required this.year,
    required this.months,
    required this.month,
  });

  factory DateModel.zero() {
    return DateModel(year: 0, months: [], month: MonthModel.zero());
  }

  bool equal(DateModel m) {
    return year == m.year && month.equal(m.month);
  }

  bool equalTo(int y, int m, int d) {
    return year == y && month.month == m && month.day == d;
  }

  DateModel newDate(int idx, int day) {
    if (month.prefixDays > idx) {
      return DateModel(
          year: lastYear,
          months: [],
          month: MonthModel(
              month: month.lastMonth, day: day, days: month.lastMonthDays));
    } else if (idx >= month.days + month.prefixDays) {
      return DateModel(
          year: nextYear,
          months: [],
          month: MonthModel(month: month.nextMonth, day: day, days: 0));
    }
    return DateModel(
        year: year,
        months: [],
        month: MonthModel(month: month.month, day: day, days: 0));
  }

  void take(DateModel md) {
    year = md.year;
    months = md.months;
    month = MonthModel(
        month: md.month.month, day: md.month.day, days: md.month.days);
  }

  bool isBefore(DateModel e) {
    if (month.day == 0) {
      return false;
    }
    if (year == e.year) {
      return month.isBefore(e.month);
    }
    if (year < e.year) {
      return true;
    }
    return false;
  }

  bool isAfter(DateModel e) {
    if (month.day == 0) {
      return false;
    }
    if (year == e.year) {
      return month.isAfter(e.month);
    }
    if (year > e.year) {
      return true;
    }
    return false;
  }
}

class MonthModel {
  int month;
  int day;
  int days;
  int startWeekday;
  int endWeekday;
  int lastMonthDays;
  //上月显示天数
  int prefixDays;
  //下一月显示天数
  int suffixDays;
  int get lastMonth {
    if (month == 1) {
      return 12;
    }
    return month - 1;
  }

  int get nextMonth {
    if (month == 12) {
      return 1;
    }
    return month + 1;
  }

  MonthModel({
    required this.month,
    required this.day,
    required this.days,
    this.startWeekday = 0,
    this.endWeekday = 0,
    this.lastMonthDays = 0,
    this.prefixDays = 0,
    this.suffixDays = 0,
  });

  factory MonthModel.zero() {
    return MonthModel(month: 0, days: 30, day: 0);
  }
  void asyncData(MonthModel m) {
    startWeekday = m.startWeekday;
    endWeekday = m.endWeekday;
    lastMonthDays = m.lastMonthDays;
  }

  bool equal(MonthModel m) {
    if (month == m.month && day == m.day) {
      return true;
    }
    return false;
  }

  bool isBefore(MonthModel e) {
    if (day == 0) {
      return false;
    }
    if (month < e.month) {
      return true;
    }
    if (month == e.month && day < e.day) {
      return true;
    }
    return false;
  }

  bool isAfter(MonthModel e) {
    if (day == 0) {
      return false;
    }
    if (month > e.month) {
      return true;
    }
    if (month == e.month && day > e.day) {
      return true;
    }
    return false;
  }
}
